//
//  LRJVideoDetailViewController.m
//  BreadTravel
//
//  Created by 李润嘉 on 15/12/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LRJVideoDetailViewController.h"
#import "LRJVideoDetailTableViewCell.h"
#import "LRJVideoPlayerViewController.h"


@interface LRJVideoDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UIImageView *photoImage;//照片


@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *myArray;
@property (nonatomic, strong)LRJVideoDetailModel *viedoDetailModel;

@end

@implementation LRJVideoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    
    //创建tableview
    [self createTableView];
    
    //初始化数组
    self.myArray = [NSMutableArray array];
    
    //请求网络数据
    [self getData];
}
//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}
#pragma mark -- 请求网络数据
- (void)getData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    
    self.urlStr = [NSString stringWithFormat:@"http://lvshiv.saygoer.com/travelVideos/%@", self.myId];
    [AFNetworkTool getUrl:self.urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSMutableDictionary *dic = result;
        self.viedoDetailModel = [LRJVideoDetailModel modelWithDictionary:dic];
        [self.myArray addObject:self.viedoDetailModel];
        
        [self createView];
        [hub hide:YES];
        [self.myTableView reloadData];
    }
     
    failure:^(NSError *error)
    {
       
        NSLog(@"error = %@", error);
        
    }];
}
//布局
- (void)createView{
    self.photoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -300.0, WIDTH, 300)];
    self.photoImage.layer.masksToBounds = YES;
    //设置imageView高度改变时宽度也跟着改变
    self.photoImage.contentMode = UIViewContentModeScaleAspectFill;
    self.photoImage.userInteractionEnabled = YES;
    NSURL *url = [NSURL URLWithString:self.viedoDetailModel.imageHref];
    [self.photoImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"gotravel.jpg"]];
    [self.myTableView addSubview:self.photoImage];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.photoImage addGestureRecognizer:tap];
    
    UIImageView *startImage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 20, self.photoImage.H / 2 - 20, 40, 40)];
    startImage.alpha = 0.8;
    startImage.image = [UIImage imageNamed:@"iconfont-bofang.png"];
    [self.photoImage addSubview:startImage];
    

}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    LRJVideoPlayerViewController *playerVC = [[LRJVideoPlayerViewController alloc] init];
    playerVC.urlStr = self.viedoDetailModel.videoHref;
    playerVC.name = self.viedoDetailModel.name;
    [self presentViewController:playerVC animated:YES completion:^{
        
        
        
    }];
}
#pragma mark -- 创建tableview
- (void)createTableView
{
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    //设置tableview的contentView距离上边界200
    //相对于0点,已经向下偏移了-200
    self.myTableView.contentInset = UIEdgeInsetsMake(300.0 , 0, 0, 0);

    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.myTableView registerClass:[LRJVideoDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
}
#pragma mark -- tableview必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    static NSString *cellIndentifier = @"cell";
    LRJVideoDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    cell.videoModel = [self.myArray objectAtIndex:indexPath.row];
    return cell;
    
}
#pragma mark -- Cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRJVideoDetailModel *videoDetailModel = [self.myArray objectAtIndex:indexPath.row];
    CGSize textSize = [SizeforLabel labelRectWithSize:CGSizeMake(WIDTH - 20, MAXFLOAT) LabelText:videoDetailModel.intro Font:[UIFont systemFontOfSize:15]];
    CGFloat height = textSize.height;
    return height + 250 ;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    //    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y ;//加上导航栏高度,第一次是-200
    //    NSLog(@"y2 === %f",y);
    
    if (y < -300) {
        CGRect frame = self.photoImage.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.photoImage.frame = frame;
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
